package util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import pojo.ConfigPoJo;
import pojo.RequestDetailPoJo;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class CompaUtil {

    private static ConfigPoJo configPoJo;

    static {
        try {
            configPoJo = ConfigUtil.getConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void fillPostCompa(List<RequestDetailPoJo> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        RequestDetailPoJo pojo = null;
        Set<String> fillSet = new LinkedHashSet<>();

        for (int i = 0; i < list.size(); i++) {
            pojo = list.get(i);

            // 筛选出以下两种情况
            // 执行成功但返回值和被对比的结果不一致
            // 执行失败，但在被对比的服务器上执行成功
            if ("SUCCESS".equals(pojo.getResultType())) {
                if (fillSet.contains(pojo.getServiceCode() + "-SUCCESS")) {
                    continue;
                }
                // 执行对比
                fillPost(pojo);
                fillSet.add(pojo.getServiceCode() + "-SUCCESS");
            } else if ("FAIL".equals(pojo.getResultType())) {
                if (fillSet.contains(pojo.getServiceCode() + "-FAIL")) {
                    continue;
                }
                // 执行对比
                fillPost(pojo);
                fillSet.add(pojo.getServiceCode() + "-FAIL");
            } else {
                if (fillSet.contains(pojo.getServiceCode() + "-ERROR")) {
                    continue;
                }
                // 执行对比
                fillPost(pojo);
                fillSet.add(pojo.getServiceCode() + "-ERROR");
            }
        }
    }

    public static void fillPost(RequestDetailPoJo requestDetailPoJo) {
        String result = null;
        try {
            long startTime = System.currentTimeMillis();
            String url = configPoJo.getCompaIp() + requestDetailPoJo.getUrl().substring(requestDetailPoJo.getUrl().indexOf("/"));

            JSONObject paramJSON = JSONObject.parseObject(requestDetailPoJo.getRequest());
            // 过滤空值
            JSONFormatUtil.removeEmpty(paramJSON);

            requestDetailPoJo.setCompa("Y");
            requestDetailPoJo.setCompaUrl(url);

            result = HttpUtil.post(url, paramJSON.toJSONString());

            requestDetailPoJo.setCompaTime(System.currentTimeMillis() - startTime);

            JSONObject json = JSONObject.parseObject(result);

            if (json.containsKey("transactionStatus")) {
                boolean success = "0".equals(json.getJSONObject("transactionStatus").getString("errorCode"));
                if (success) {
                    requestDetailPoJo.setCompaResultType("SUCCESS");
                } else {
                    requestDetailPoJo.setCompaResultType("FAIL");
                }
            } else {
                requestDetailPoJo.setCompaResultType("ERROR");
            }
            requestDetailPoJo.setCompaResult(json.toJSONString());
        } catch (Throwable e) {
            requestDetailPoJo.setCompaResult(result);
            requestDetailPoJo.setCompaResultType("ERROR");
        }
        // 预设为Y
        requestDetailPoJo.setCompaPass("Y");
        // 如果ResultType不一致，则直接赋值
        if (!requestDetailPoJo.getResultType().equals(requestDetailPoJo.getCompaResultType())) {
            requestDetailPoJo.setCompaPass("N");
        } else if ("SUCCESS".equals(requestDetailPoJo.getResultType()) && "SUCCESS".equals(requestDetailPoJo.getCompaResultType())) {
            JSONObject compaResultJSON = JSONObject.parseObject(requestDetailPoJo.getCompaResult());
            // 遍历每个value , 找出左边有值右边为null的情况
            JSONObject resultJSON = JSONObject.parseObject(requestDetailPoJo.getResult());
            for (Object key : compaResultJSON.keySet()) {
                Object leftValue = compaResultJSON.get(key);
                Object rightValue = resultJSON.get(key);
                if (leftValue == null) {
                    continue;
                }
                if (rightValue == null) {
                    requestDetailPoJo.setCompaPass("N");
                }
                if (leftValue instanceof JSONArray) {
                    if (((JSONArray) leftValue).size() > 0 && ( (rightValue == null) || !(rightValue instanceof JSONArray) || ((JSONArray) leftValue).size() < 1)) {
                        requestDetailPoJo.setCompaPass("N");
                    }
                } else {
                    if (!StringUtils.isEmpty(leftValue) && StringUtils.isEmpty(rightValue)) {
                        requestDetailPoJo.setCompaPass("N");
                    }
                }
            }
        }

    }
}
